package cn.zyq.ykb.service.product.impl;

import cn.zyq.ykb.beans.product.Product;
import cn.zyq.ykb.mapper.product.ProductMapper;
import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.product.ProductTypeMapper;
import cn.zyq.ykb.beans.product.ProductType;
import cn.zyq.ykb.service.product.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * 商品类型Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-11-02
 */
@Service
public class ProductTypeServiceImpl  extends BaseService implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询商品类型
     *
     * @param productTypeID 商品类型主键
     * @return 商品类型
     */
    @Override
    public ProductType selectProductTypeByProductTypeID(Integer productTypeID) {
        ProductType productType = productTypeMapper.selectByPrimaryKey(productTypeID);
        if (productType.getDeleted() == 1) {
            return null;
        }
        return productType;
    }

    /**
     * 查询商品类型
     *
     * @param serviceID 业务ID
     * @return 商品类型
     */
    @Override
    public ProductType selectProductTypeByServiceID(String serviceID) {
        ProductType param = new ProductType();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.productTypeMapper.selectOne(param);
    }

    /**
     * 查询商品类型列表
     *
     * @param pageParam
     * @return 商品类型
     */
    @Override
    public PageInfo<ProductType> selectProductTypeList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<ProductType> list = this.productTypeMapper.list(pageParam.getKv());
        list.forEach(p->{
            Product product = new Product();
            product.setProductTypeID(p.getProductTypeID());
            product.setDeleted(0);
            int count = this.productMapper.selectCount(product);
            p.setNumber(count);
        });
        return new PageInfo<>(list);
    }

    /**
     * 查询商品类型列表
     *
     * @param pageParam
     * @return 商品类型
     */
    @Override
    public List<ProductType> selectProductTypeAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.productTypeMapper.list(pageParam.getKv());
    }
    /**
     * 新增商品类型
     *
     * @param param 商品类型
     * @return 结果
     */
    @Override
    public int insertProductType(ProductType param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.productTypeMapper.insertSelective(param);
    }

    /**
     * 修改商品类型
     *
     * @param param 商品类型
     * @return 结果
     */
    @Override
    public int updateProductType(ProductType param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.productTypeMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除商品类型
     *
     * @param productTypeIDs 需要删除的商品类型主键
     * @return 结果
     */
    @Override
    public int deleteProductTypeByProductTypeIDs(Integer[] productTypeIDs) {
        int i = 0;
        for (Integer productTypeID : productTypeIDs) {
            i += this.deleteProductTypeByProductTypeID(productTypeID);
        }
        return i;
    }

    /**
     * 删除商品类型信息
     *
     * @param productTypeID 商品类型主键
     * @return 结果
     */
    @Override
    public int deleteProductTypeByProductTypeID(Integer productTypeID) {
        ProductType productType = this.selectProductTypeByProductTypeID(productTypeID);
        if (productType == null) {
            return 0;
        } else {
            ProductType storeInfo = new ProductType();
            storeInfo.setProductTypeID(productTypeID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.productTypeMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
